#include<graphics.h>
#include<conio.h>
#include<stdio.h>
int main()
{
	float width,height;//定义屏幕的宽和高//
	width=600;
	height=400;
    initgraph(width,height);

	
	float ball_x,ball_y,ball_vy,gravity,radius;
	radius=20;
	ball_x=width/4;
	ball_y=height-radius;
	ball_vy=0;
	gravity=0.5;

	float rect_left_x,rect_top_y,rect_width,rect_height,rect_vx;
	rect_left_x=width*3/4;
	rect_top_y=height-rect_height;
	rect_width=20;
    rect_height=100;
	rect_vx=-3;

    float rect_left_x1,rect_top_y1,rect_width1,rect_height1,rect_vx1;
	rect_left_x1=width*3/4;
	rect_top_y1=0;
	rect_width1=20;
    rect_height1=100;
	rect_vx1=-3;

	int score=0;


	while(1)
	{
		if(kbhit())//当按下键时循环//
		{
			char input;
		input=_getch();//按键//
		if(input==' ')
		{
			ball_vy=-16;
		
		}
		}

		ball_vy=ball_vy+gravity;
		ball_y=ball_y+ball_vy;
		if(ball_y>=height-radius)
		{
			ball_vy=0;
			ball_y=height-radius;
		}
		rect_left_x=rect_left_x+rect_vx;
        rect_left_x1=rect_left_x1+rect_vx1;
		if(rect_left_x<=0)
		{
			score++;
			rect_left_x=width;
            rect_left_x1=width;
			rect_height=rand()%int(height/4)+height/4;
            rect_height1=rand()%int(height/4)+height/4;
			rect_vx=rand()/float(RAND_MAX)*4-7; 
		    rect_vx1=rect_vx;
        }
		
        if((ball_x+radius>=rect_left_x
			&&ball_y+radius>=height-rect_height
			&&ball_x-radius<=rect_left_x+rect_width)
			||(ball_x+radius>=rect_left_x1
			&&ball_y-radius<=rect_height1
			&&ball_x-radius<=rect_left_x1+rect_width1))
		{
			score=0;
			Sleep(100);
		}
            if(ball_y<=radius)
		{
			ball_y=radius;
			ball_vy=0;
		}
			cleardevice();//清空画面//
			fillrectangle(rect_left_x,height-rect_height,rect_left_x+rect_width,height);
            fillrectangle(rect_left_x1,0,rect_left_x1+rect_width1,rect_height1);
	fillcircle(ball_x,ball_y,radius);//绘制小球//
	TCHAR s[20];
	_stprintf(s,_T("%d"), score);
	settextstyle(40,0,_T("宋体"));
	outtextxy(50,30,s);



	Sleep(10);//暂停十秒钟
	}

	_getch();
	closegraph();
	return 0;
} 